$(function(){
    loadFoot(0);
    share();
    var index=0;
    var size=10;
    var message=0;
    // var fid=ddid||did;//第一次进来时间初始化的第一列表id  first id
    $('section').dropload({
        scrollArea:window,
        distance:50,
        domUp:{
            domClass : 'dropload-up',
            domRefresh : '<div class="dropload-refresh">↓下拉刷新</div>',
            domUpdate : '<div class="dropload-update">↑释放更新</div>',
            domLoad : '<div class="dropload-load"><span class="loading"></span>加载中...</div>'
        },
        domDown:{
            domClass : 'dropload-down',
            domRefresh : '<div class="dropload-refresh">↑上拉加载更多</div>',
            domLoad : '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
            domNoData : '<div class="dropload-noData">已经到底了，别扯了</div>'
        },
        loadDownFn:function(me){
            index++;
            message++;
            $.ajax({
                url:host+'/CMSAPI/GetPartnerOrderList?openid='+openid+'&cid='+cid+'&index='+index+'&size='+size,
                type:'GET',
                dataType:'JSON',
                success:function(data){
                    if(data.state){
                        var msgNum =Math.ceil(data.data.count/size);
                        if (message <=msgNum){
                            var html3='';
                            var list3=data.data.OrderList;
                            $.each(list3,function(i,obj){
                                var RealName=obj.buy_name||'';
                                var productName=obj.name||'';
                                var orderState=obj.order_state||'';
                                var orderNum=obj.order_id||'';
                                var orderTime=obj.order_time||'';
                                var orderPrice=obj.price||'';
                                var orderCount=obj.count||'1';
                                var order_id=obj.order_id||'';
                                var dataId=obj.id;
                                html3+='<li data-id="'+order_id+'"><div class="detail"><div class="orderDetail"><div class="orderTitle overhide">'
                                html3+='    <p class="t1">'+productName+'</p>'
                                html3+='    <p class="t2">'+orderState+'</p>'
                                html3+='    </div><div class="orderState overhide">'
                                html3+='    <p class="t1">'+orderNum+'</p>'
                                html3+='    <p class="t2">'+orderTime+'</p>'
                                html3+='    </div></div><div class="overhide"><div class="clientName">'
                                html3+='     <span>'+RealName+'</span><br><span class="detailTip">点击列表查看订单详情</span>'
                                html3+='    </div><div class="priceTotal"><p>￥'+orderPrice+'</p>'
                                html3+='<p style="color: #aaa">×'+orderCount+'</p></div></div></div></li>'
                            });
                            $('.buyInfo').append(html3);
                        }else {
                            me.lock();
                            me.noData();
                        }
                    }else {
                        alert('网络繁忙，请稍后重试！')
                    }
                    me.resetload();
                }
            })
        }
        // loadUpFn:function(me){
        //     location.reload();
        //     me.resetload();
        // }
    });
    //TODO:给订单列表添加点击事件(大图展示)
    var openPhotoSwipe = function(items) {
        var pswpElement = document.querySelectorAll('.pswp')[0];
        // build items array
        // define options (if needed)
        var options = {
            // history & focus options are disabled on CodePen
            history: true,
            focus: true,
            showAnimationDuration: 0,
            hideAnimationDuration: 0

        };
        var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
        gallery.init();
    };
    $('.buyInfo').on('click','li',function(){
        var id=$(this).attr('data-id');
        $.ajax({
            url:host+'/CMSAPI/GetOrderDetail?id='+id,
            success:function(data){
                if(data.state){
                    var imgs=data.data.images;
                    if(imgs.length>0){
                        var photoSwiperItems=[];
                        $.each(imgs,function(i,img){
                            var url=img.url;
                            var w=img.width;
                            var h=img.height;
                            photoSwiperItems.push({src:url,w:w,h:h});
                        });
                        openPhotoSwipe(photoSwiperItems);
                    }else {
                        $.Dialog({
                            type:'alert',
                            content:'该订单没有图片！',
                            sureColor:'#6FA8EB'
                        })
                    }
                }
            }
        })
    })
});